package May._02.homework.work5;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        String[] words=new String[]{"cat", "bt", "hat", "tree"};
        String str="atach";
        //以str的字符为key、出现的次数为value
        HashMap<Character, Integer> strMap = new HashMap<>();
//        Iterator<Map.Entry<Character, Integer>> iteratorStr = strMap.entrySet().iterator();
        for (int i = 0; i < str.length(); i++) {
            Integer strMapOrDefault = strMap.getOrDefault(str.charAt(i), 0);
            strMap.put(str.charAt(i),strMapOrDefault+1);
        }
        int count=0;
        //对words中的每个单词都如法炮制
        for (String word : words) {
            HashMap<Character, Integer> wordMap = new HashMap<>();
            int index=0;
            for (int i = 0; i < word.length(); i++) {
                Integer wordMapOrDefault = wordMap.getOrDefault(word.charAt(index), 0);
                wordMap.put(word.charAt(index),wordMapOrDefault+1);
                index++;
            }
            boolean flag=false;
            Iterator<Map.Entry<Character, Integer>> wordEntrySet = wordMap.entrySet().iterator();
            while (wordEntrySet.hasNext()){
                Map.Entry<Character, Integer> next = wordEntrySet.next();
                if(strMap.get(next.getKey())!=null&&next.getValue()<=strMap.get(next.getKey())){
                    flag=true;
                }else {
                    flag=false;
                    break;
                }
            }
            if(flag){
                count+=word.length();
            }
        }
        System.out.println("count = " + count);

    }
}
